﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpExperiments.Nullsafety
{
	public static class DoMaybeGeneralExtensions
	{
		public static B DoMaybe<A, B>(this A obj, Func<A, B> func)
		{
			return (Maybe.IsMaybeable(typeof(A)) && Object.Equals(obj, default(A))) ? default(B) : func(obj);
		}

		public static B DoMaybe<A, B>(this A obj, Func<A, B> func, B defaultValue)
		{
			return (Maybe.IsMaybeable(typeof(A)) && Object.Equals(obj, default(A))) ? defaultValue : func(obj);
		}

		public static A DoMaybe<A>(this A obj, Action<A> func)
		{
			if (! Maybe.IsMaybeable(typeof(A)) || ! Object.Equals(obj, default(A))) func(obj);
			return obj;
		}

		public static A DoMaybe<A>(this A obj, Action<A> func, Action defaultFunc)
		{
			if (! Maybe.IsMaybeable(typeof(A)) || ! Object.Equals(obj, default(A))) func(obj);
			else defaultFunc();
			return obj;
		}
	}
}
